package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author stone
 * @date 2022/07/12 11:11
 */
@RestController
@RequestMapping("upload")
public class UploadController {

    @RequestMapping("file")
    public BaseRespVo uploadFile(MultipartFile file) throws IOException {

        //MultipartFile提供的方法获得信息
        String originalFilename = file.getOriginalFilename(); //原始的文件名
        String[] split = originalFilename.split("\\.");
        if (split != null && split.length > 1) {
            String suffix = split[split.length - 1];
        }

        long size = file.getSize(); //文件大小
        String contentType = file.getContentType(); //正文类型 → image/jpeg  */*

        // 可以直接保存 → 你告知它文件保存的位置就可以了
        // 需求：上传bingbing.jpg → D:\tmp
        //File saveFile = new File("D:\\tmp", "bingbing.jpg");
        File saveFile = new File("D:\\tmp", originalFilename);
        file.transferTo(saveFile);

        return BaseRespVo.ok(null);
    }


    @RequestMapping("files")
    public BaseRespVo uploadFile(MultipartFile[] files) throws IOException {
        for (MultipartFile file : files) {
            String originalFilename = file.getOriginalFilename();
            File saveFile = new File("D:\\tmp", originalFilename);
            file.transferTo(saveFile);
        }
        return BaseRespVo.ok(null);
    }
}
